package com.example.mybatisplues.controller;

import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplues.po.PageResult;
import com.example.mybatisplues.po.User;
import com.example.mybatisplues.po.UserPage;
import com.example.mybatisplues.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping
    public void addUser(@RequestBody User user) {
        userService.save(user);
    }

    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable Integer id) {
        boolean b =  userService.removeById(id);
        System.out.println(b);
    }

    @GetMapping("/{id}")
    public User getById(@PathVariable Integer id) {
        User user = userService.getAddressAndUserbyid(id);
        return user;
    }

    @GetMapping
    public List<User> getByIds(@RequestParam List<Integer> ids) {
        List<User> users = userService.getAddressAndUserbyids(ids);
        users.forEach(System.out::println);
        return users;
    }

    @PutMapping("/{id}/deduction/{money}")
    public void sellGoods(@PathVariable Integer id,@PathVariable Integer money){

userService.deductMoneyById(id,money);
    }
    @GetMapping("/list")
    public List<User> getByCon(String name,Integer status,Integer minBalance,Integer maxBalance){
        List<User> list = userService.queryUserlist(name,status,minBalance,maxBalance);
        return list;
    }

@GetMapping("/page")
    public PageResult UserPage( UserPage userPage){
     return    userService.queryPage(userPage);
}
}
